#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <errno.h>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include "pool.h"

using namespace std;

#define EP_SIZE 1024

/**
 * @brief 单例模式
 * 
 */
class Epoll
{

private:

    unsigned int Max_Listeners;
    unsigned int Cur_Listeners;
    int sockfd,newconnetfd,epollfd;
    int ep_size = EP_SIZE;
    int res;

    struct epoll_event midep, *epbuf;

    socklen_t len;
    struct sockaddr_in cliaddr, servaddr;

    Epoll(in_addr_t addr,in_port_t port,sa_family_t family);
    
    static Epoll *EpollInstance;
    static Pool  *pool;

public:
    
    Epoll(const Epoll&)=delete;
    void operator=(const Epoll&) = delete;

    static Epoll* GetInstance(string addr="192.168.85.128",uint16_t port=23625, sa_family_t family=AF_INET);
    

    void Printf_Info(struct sockaddr_in& info)const;
    void Printf_Info(int fd)const;

    void Bind(in_addr_t addr,in_port_t port,sa_family_t family);
    void ListensEvents(void);
    bool Add_Events(int fd, int status);
    bool Del_Events(int fd);
    bool Events_Handled(struct epoll_event& epevent);


    unsigned int Get_Cur_Listeners(void){return this->Cur_Listeners;};
    unsigned int Get_Max_Listeners(void){return this->Max_Listeners;};

    ~Epoll();
};


